package demo1;

import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Administrator
 * Date: 2023-07-06
 * Time: 13:45
 */
public class SeqList {
    private int[] arry;
    private int mysize;


    public boolean contains(int toFind){
        for (int i = 0; i <this.mysize ; i++) {
            if(arry[i] == toFind){
                return true;
            }
        }
        return false;
    }

    public int indexOf(int toFind){
        for (int i = 0; i <this.mysize ; i++) {
            if(arry[i] == toFind){
                return i;
            }
        }
        return -1;
    }

    public int get(int pos){
        if(!checkpos(pos)){
            throw new PosOutBoundsException("get 获取数据时位置不合法");
        }
        return arry[pos];
    }

    public int size(){
        return this.mysize;
    }

    private boolean checkpos(int pos){
        if(pos < 0 || pos >= mysize){
            return false;
        }
        return true;
    }
    public void set(int pos,int value){
        if(!checkpos(pos)){
            throw new PosOutBoundsException("get 获取数据时位置不合法");
        }
        this.arry[pos] = value;
    }

    public boolean isFull(){
        return mysize == arry.length;
    }
    private void resize(){
        arry = Arrays.copyOf(arry,2*arry.length);
    }
    public void add(int pos,int data){
        if(pos < 0 || pos > mysize){
            throw new PosOutBoundsException("add元素时 pos位置不合法");
        }
        if(isFull()){
            resize();
        }
        for (int i = mysize-1; i >= pos ; i--) {
            this.arry[i+1] = this.arry[i];
        }
        this.arry[pos] = data;
        this.mysize++;
    }
    private boolean isEmpty(){
        return mysize == 0;
    }
    public void remove(int toRemove){
        if(isEmpty()){
            return;
        }

        int index = indexOf(toRemove);

        if(index == -1){
            return;
        }

        for (int i = index; i <mysize-1; i++) {
            this.arry[index] = this.arry[index + 1];
        }
        mysize--;
    }

    public void clear(){
        for (int i = 0; i < mysize; i++) {
            //this.arry[i] = null;
        }
        mysize = 0;
    }
}
